using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Cleverscape.UTorrentClient.WebClient;

namespace uTorrentNetClient.ModelDesign.Converter
{
    [ValueConversion(typeof(object), typeof(bool))]
    public class ObjectBoolConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(bool?))
            {
                return false;
            }
            if (value is object)
            {
                var res = false;
                bool.TryParse(value.ToString(), out res);

                return res;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(object))
            {
                return null;
            }
            if (value is bool)
            {
                return value;
            }
            return null;
        }
    }
}
